var map, vectorLayer, drawController;
var WGS_PROJECTION = new OpenLayers.Projection("EPSG:4326");
var SPHERICAL_MERCATOR_PROJECTION = new OpenLayers.Projection("EPSG:900913");

function init() {
	
	//MAP
	map = new OpenLayers.Map("map", {
		displayProjection: WGS_PROJECTION,
		restrictedExtent: new OpenLayers.Bounds(116.27, 4.39, 127, 21.53).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION)
	});
    var osm = new OpenLayers.Layer.Google('Google Map Layer', {});
    
    //STYLING
    var defaultStyle = new OpenLayers.Style({
        'pointRadius': 15,
        'fillColor': '#368c00', 
        'strokeColor': '#ffffff',
        'strokeWidth': 5
    });
    
    var styleMap = new OpenLayers.StyleMap({'default': defaultStyle});
	
    //CONTROLS
	vectorLayer = new OpenLayers.Layer.Vector("Reports");
	map.addLayers([osm, vectorLayer]);
    vectorLayer.styleMap = styleMap;

	drawController = new OpenLayers.Control.DrawFeature(vectorLayer, OpenLayers.Handler.Point, {featureAdded: onFeatureAdded});
	map.addControl(drawController);
	map.addControl(new OpenLayers.Control.MousePosition());
	
	map.setCenter(new OpenLayers.LonLat(121.06850, 14.64866).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION), 14);
}

function setMapCenter(){
    map.setCenter(new OpenLayers.LonLat(lon, lat).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION), 17);
}

function activateDrawFeature(){
	drawController.activate();
}

function onFeatureAdded(feature){
    vectorLayer.removeAllFeatures();
    vectorLayer.addFeatures(feature);
    mapPoint = new OpenLayers.Geometry.Point(feature.geometry.getBounds().getCenterLonLat().lon, feature.geometry.getBounds().getCenterLonLat().lat).transform(SPHERICAL_MERCATOR_PROJECTION, WGS_PROJECTION);
    lon = mapPoint.x;
    lat = mapPoint.y;
}


